<template>
	<view class="u-f-c u-f-ajc content">倒计时：{{ msg }}</view>
</template>

<script>
export default {
	data() {
		return {
			// orderTime: 1609744503000, //订单创建时间
			orderTime: 1609902903000, //订单创建时间
			endTime: 30 * 1000 * 60, // 倒计时 1个小时
			currentTime: Date.now(),
			timer: null,
			msg: ''
		};
	},
	onLoad() {
		let totalTime = this.orderTime + this.endTime;
		// console.log('总时间：', totalTime);
		// console.log('当前时间戳：', this.currentTime);
		let temp = totalTime - this.currentTime;
		if (temp >= 1000) {
			// 必须大于1s
			this.countDown(Math.ceil(temp / 1000));
		} else {
			clearInterval(this.timer);
			// this.$global.msg('倒计时结束');
		}
	},
	methods: {
		countDown(temp) {
			if (this.timer) {
				return clearInterval(this.timer);
			}
			this.timer = setInterval(() => {
				temp--;
				// 计算出 天时分秒
				let day = parseInt(temp / 60 / 60 / 24);
				let hr = parseInt((temp / 60 / 60) % 24);
				let min = parseInt((temp / 60) % 60);
				let sec = parseInt(temp % 60);
				hr = hr > 9 ? hr : '0' + hr;
				min = min > 9 ? min : '0' + min;
				sec = sec > 9 ? sec : '0' + sec;
				this.msg = day + '天' + hr + '时' + min + '分' + sec + '秒';
				
				if (temp <= 0) {
					// this.$global.msg('倒计时结束');
					// 停止计时
					return clearInterval(this.timer);
				}
			}, 1000);
		}
	}
};
</script>

<style lang="scss" scoped>
.content {
	text-align: center;
	padding-top: 20vh;
}
</style>
